﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.ComponentModel;

namespace KinBot_Controller_V2
{
   public class fezConnectt
    {
       
            public Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            public IPEndPoint kinBot = new IPEndPoint(IPAddress.Any, 2000);


            public void start()
            {
                //*** New style of connecting to an end point. This is the same style that is implemented on windows 8 metro client
                // ** provides faster access to the network by setting the networking on a seperate thread from the GUI.
                //** Reference: http://www.silverlightshow.net/items/Implements-network-protocol-easily-with-a-generic-SocketClient.aspx
                //** Reference: http://social.msdn.microsoft.com/forums/en-us/wpdevelop/thread/36176f3b-d274-4337-9200-f4bb36318323


                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.2.200"), 2000);
                args.Completed += new EventHandler<SocketAsyncEventArgs>(Operation_Completed);
                mySocket.ConnectAsync(args);
                //this.mySocket.Bind(kinBot);

            }
            void Operation_Completed(object sender, SocketAsyncEventArgs e)
            {
                if (e.SocketError != SocketError.Success)
                {
                    Console.Write("Did not connect to the KinBot " + e.LastOperation.ToString());
                }
                else if (e.LastOperation == SocketAsyncOperation.Connect)
                {
                    App.kinbotKinected = true;
                    Console.Write("Connected to KinBot");
                }

            }
            public void sendData(string send)
            {

                byte[] data = Encoding.UTF8.GetBytes(send);

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.2.200"), 2000);

                args.SetBuffer(data, 0, data.Length);


                mySocket.SendToAsync(args);


            }
            public void sendData(byte[] send)
            {
                string incomingMsg;
                while (mySocket.Poll(200, SelectMode.SelectRead))
                {
                    if (mySocket.Available > 0)
                    {
                        //headData.Text = (send);
                        byte[] inBuf = new byte[mySocket.Available];
                        EndPoint recEndPoint = new IPEndPoint(IPAddress.Any, 0);
                        mySocket.ReceiveFrom(inBuf, ref recEndPoint);
                        // fezMessage.Text = ("Message From" + ((IPEndPoint)recEndPoint).Address.ToString());
                        incomingMsg = (new string(Encoding.UTF8.GetChars(inBuf)));




                        //byte[] bytesToSend = Encoding.UTF8.GetBytes(send);

                        // byte[] bytesToSend = BitConverter.GetBytes(ledDimmerValue);
                        mySocket.SendTo(send, send.Length, SocketFlags.None, (IPEndPoint)recEndPoint);

                        if (incomingMsg != null)
                        {

                        }
                    }

                }
            }
        }

    }

